   //初始化数据
var pnum = "";   
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findDesignCellGroups",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data.list != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"design_cell_group_code": "设计单元组编号"
		          			      	,"design_cell_group_name": "设计单元组名称"
		          			      	,"project_design_phase_name":"设计阶段"
		          			      	,"project_design_content_name":"设计内容"
		          			      	,"project_design_major_name":"设计专业"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"更新时间"
		          			      	,"operation":"操作"
		            			},
		            			"titleCheckbox":false,
								"btn":[{"event":"updateObj","name":"编辑"},
								       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"initData");
	        	}

	        },
	        error: function () {
	        }
	    });
   }


   var searchObj = function (num) {
       if (num==undefined|| num==""||num.type=="click" ) {
           num=1;
       }
       var name=$("#searchName").val();
       var param = {
           page_num:num,
           page_size:"10",
           design_cell_group_name:name
       }
       $.ajax({
           url: base + "/dataCenter/businessdata/findDesignCellGroups",
           data: param,
           success: function (data, textStatus, jqXHR) {
               if(data.list != null){
                   var pageCount = data.pager.pageCount;
                   var dataObj ={
                       "list":data.list,
                       "dataTool":{
                           "title":{
	          			      	"design_cell_group_code": "设计单元组编号"
		          			      	,"design_cell_group_name": "设计单元组名称"
		          			      	,"project_design_phase_name":"设计阶段"
		          			      	,"project_design_content_name":"设计内容"
		          			      	,"project_design_major_name":"设计专业"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"更新时间"
		          			      	,"operation":"操作"
                           },
                           "titleCheckbox":false,
                           "btn":[{"event":"updateObj","name":"编辑"},
                               {"event":"delObj","name":"删除"}]
                       }
                   };
                   showPageTable('view', dataObj,num,pageCount,"searchObj");
               }

           },
           error: function () {
           }
       });
   }
   
   //删除设计单元
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var id = obj.id,code = obj.design_cell_table_code
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delDesignCellById/"+id+"/"+code,
			   data:{},
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   //添加设计单元资料
   var addObj = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "design_cell_table_code":"设计单元编号",
					      	"design_cell_table_name": "设计单元名称",
					      	"project_design_phase_id":"所属设计阶段",
					      	"project_design_major_id":"所属专业",
					      	"project_design_content_id":"所属设计内容",
                        	"isShow":"是否显示"
					},"text":{
						    "design_cell_table_code":""
						    ,"design_cell_table_name":""
							,"project_design_phase_id":""
							,"project_design_major_id":""	
							,"project_design_content_id":""
							,"isShow":""
					},"input":{
						    "design_cell_table_code":"text"
						    ,"design_cell_table_name":"text"
							,"project_design_phase_id":"select"
							,"project_design_major_id":"new_select"
							,"project_design_content_id":"select"
							,"isShow":"switch"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}

		layopen(contentDiv());
		initPhase();
		initDesignMajor();
		chanageMajorselect();
        initSwitch();
   }

   //监听专业下拉选
   var chanageMajorselect = function(){
   		form.on("select(new_select)",function(value){
   			var major_id = value.value;
   			initContent(major_id);
   		});
   }
   
   var initContent = function(major_id){
 	   $.post(base+"/dataCenter/businessdata/findDesignContent",{major_id:major_id},
 	   		  	function(data,status){
 	     		    	if(data!=''){
 	   	      		  	 var optionstring = "";
 	   	                 $.each(data.list, function(i,item){
 	   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
 	   	                });
 	   	                $("select[name='project_design_content_id']").html('<option value=""></option>' + optionstring);
 	   	      		}else{
 	   	      		  	$("select[name='project_design_content_id']").html('<option value=""></option>');
 	   	      		}
 	     		    form.render(); 
 	  });
   }
    
    var initDesignMajor = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
    		  	function(data,status){
      		    	if(data!=''){
    	      		  	 var optionstring = "";
    	                 $.each(data.list, function(i,item){
    	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
    	                });
    	                $("select[name='project_design_major_id']").html('<option value=""></option>' + optionstring);
    	      		}else{
    	      		  	$("select[name='project_design_major_id']").html('<option value=""></option>');
    	      		}
      		    form.render(); 
   });
   }
    
    var initPhase = function(){
 	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
 	   		  	function(data,status){
     		    	if(data != null && data.list != null){
   	      		  	 var optionstring = "";
   	                 $.each(data.list, function(i,item){
   	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
   	                });
   	                $("select[name='project_design_phase_id']").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("select[name='project_design_phase_id']").html('<option value=""></option>');
   	      		}
     		    form.render(); 
 	  });
    }





   //编辑资料
   var updateObj = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"主键",
						    "design_cell_table_code":"设计单元编号",
					      	"design_cell_table_name": "设计单元名称",
					      	"project_design_phase_name":"所属设计阶段",
					      	"project_design_major_name":"所属专业",
					      	"project_design_content_name":"所属设计内容",
                            "isShow":"是否显示"
					},"text":{
						    "id":obj.id
						    ,"design_cell_table_code":obj.design_cell_table_code
						    ,"design_cell_table_name":obj.design_cell_table_name
							,"project_design_phase_name":obj.project_design_phase_name
							,"project_design_major_name":obj.project_design_major_name
							,"project_design_content_name":obj.project_design_content_name
                            , "isShow":""
					},"input":{
						    "id":"hidden"
						    ,"design_cell_table_code":"text"
						    ,"design_cell_table_name":"text"
							,"project_design_phase_name":"text_readonly"
							,"project_design_major_name":"text_readonly"
							,"project_design_content_name":"text_readonly"
                            ,"isShow":"switch"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
   }
   
   //渲染弹窗
   var layopen = function(content){
		layer.open({
		  	title:'更新设计单元'
			,content: content
	    	,area: ['800px', '600px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
  		  	    var data = $(layero).find("form").serializeJSON();
  		  	    if(!verify_data(data)){
  		  	    	return false;
  		  	    }
                if(data.isShow == "on"){
                    data.isShow = true;
                }else{
                    data.isShow = false;
                }

				$.post(base+"/dataCenter/businessdata/updateDesignCellInfo",{
					id:data.id==null?"":data.id,
					design_cell_table_code:$.trim(data.design_cell_table_code),
					design_cell_table_name:$.trim(data.design_cell_table_name),
					project_design_phase_id:data.project_design_phase_id,
					project_design_major_id:data.project_design_major_id,
					project_design_content_id:data.project_design_content_id,
                    isShow:data.isShow
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }

   
   //校验数据
   var verify_data = function(data){
		if(data.design_cell_table_code == ""){
			var obj = $("input[name='design_cell_table_code']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.design_cell_table_name == ""){
			var obj = $("input[name='design_cell_table_name']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.project_design_phase_id == ""){
			alert("请选择设计阶段!");
			return false;
		}
		if(data.project_design_major_id == ""){
			alert("请选择设计专业");
			return false;
		}
		if(data.project_design_content_id == ""){
			alert("请选择设计内容!");
			return false
		}
		return true;
   }
   
   $(document).ready(function(){
	   initData(1);
   });
   